package com.pfc.others;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class SQLiteManagement{
	
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private static final String DATABASE_SELECT_LOCATIONS = 
			"SELECT locationName FROM location";
	
	
	
	public SQLiteManagement(Context context){
		dbHelper = new MySQLiteHelper(context);
	}
	
	public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	}
	
	public void close() {
	    dbHelper.close();
	}
	

	public double insertFingerprintElement(String bssid, String place, String loc, double media, double s, int n) {
	    ContentValues values = new ContentValues();
	    values.put("bssid", bssid);
	    values.put("place", place);
	    values.put("locationName", loc);
	    values.put("media", media);
	    values.put("s", s);
	    values.put("n", n);
	    double insertId = database.insert("fingerprint", "vacio", values);	    
	    return insertId;
	  }
	
	public double insertFingerprint(Fingerprint huella, String location){
		Hashtable<String, FingerprintData> hashHuella = huella.getHuella();
		Enumeration<String> enumHuella = hashHuella.keys();
		String place = huella.getPlace();
		String bssid = "";
		double media, s, insertId=0;
		int n;
		while (enumHuella.hasMoreElements()){
			bssid = enumHuella.nextElement();
			FingerprintData datos = huella.getDatos(bssid);
			media = datos.getMedia();
			s = datos.getS();
			n = datos.getN();
			System.out.println("bssid: "+bssid+"  place: "+place+"  location: "+location+"  media: "+media+"  s: "+s+"  n: "+n);
			insertId = insertFingerprintElement(bssid, place, location, media, s, n);
			//if (insertId==-1)
			//	return insertId;
		}
		return insertId;
	}
	
	public Fingerprint getFingerprint(String location, String  place){
		FingerprintData data = new FingerprintData();
		Fingerprint fingerprint = new Fingerprint(place);
		String sql = "SELECT bssid, media, s, n FROM fingerprint WHERE place='"+place+"' AND locationName='"+location+"'";  
		Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();
	    while (!cursor.isAfterLast()){
	    	String bssid = cursor.getString(0);
	    	double media = cursor.getDouble(1);
	    	double s = cursor.getDouble(2);
	    	int n = cursor.getInt(3);
	    	data.setMedia(media);
	    	data.setS(s);
	    	data.setN(n);
	    	fingerprint.put(bssid, data);
	    	cursor.moveToNext();
	    }
	    cursor.close();
	    return fingerprint;
	}
	
	public Location getLocation(String locationName){
		Hashtable<String, Fingerprint> fingerprintList = new Hashtable<String, Fingerprint>();
		ArrayList<String> placesList = getPlacesNameByLocation(locationName);

		for (int i=0 ; i<placesList.size() ; i++){
			String place = placesList.get(i);
			Fingerprint f = getFingerprint(locationName, place);
			fingerprintList.put(place, f);
		}
		Location location = new Location(locationName, fingerprintList);
		return location;
	}
	
	public double insertNewLocation(String loc, double lon, double lat){
		ContentValues values = new ContentValues();
		System.out.println("Insert location:"+ loc+", lon: " + lon + ", lat: " +lat);
	    values.put("locationName", loc);
	    values.put("longitude", lon);
	    values.put("latitude", lat);
	    double insertId = database.insert("location", null, values);
	    return insertId;
	}

	public ArrayList<String> getLocationsName(){
		Cursor cursor = database.rawQuery(DATABASE_SELECT_LOCATIONS, null);
		ArrayList<String> list = new ArrayList<String>();
		cursor.moveToFirst();
	    while (!cursor.isAfterLast()){
	    	list.add(cursor.getString(0));
	    	cursor.moveToNext();
	    }
	    cursor.close();
	    return list;
	}
	
	public ArrayList<String> getPlacesNameByLocation(String loc){
		String sql = "SELECT place FROM fingerprint WHERE locationName='"+loc+"' GROUP BY place";
		Cursor cursor = database.rawQuery(sql, null);
		ArrayList<String> list = new ArrayList<String>();
		cursor.moveToFirst();
	    while (!cursor.isAfterLast()){
	    	list.add(cursor.getString(0));
	    	cursor.moveToNext();
	    }
	    cursor.close();
	    return list;
	}
	
	public double[] getCoordinatesFromLocation(String loc){
		String sql = "SELECT longitude, latitude FROM location WHERE locationName='"+loc+"'";
		double[] coord = new double[2];
		Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();
	    coord[0] = cursor.getDouble(0);		// longitude
	    coord[1] = cursor.getDouble(1);		// latitude
	    cursor.close();
	    return coord;
	}
	
	public double deletePlaceFromLocation(String loc, String place){
		double result = 0;
		result = database.delete("fingerprint","(place='"+place+"' AND locationName='"+loc+"')", null);
		return result;
	}
	
	public double deleteLocation(String loc){
		double result = 0;
		result = database.delete("location","locationName='"+loc+"'", null);
		return result;
	}
	
	public void deleteAll(){
		database.delete("fingerprint",null, null);
		database.delete("location",null, null);
	}
	
	
	
}

